package ro.ubbcluj.cs.damate.persistence.entities;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Transient;

import static javax.persistence.CascadeType.*;
import static javax.persistence.FetchType.*;
import static ro.ubbcluj.cs.damate.utils.EntityUtils.*;

/**
 * @author Lóránd Fazakas (lorand)
 *         Date: 4/22/12
 *         Time: 1:12 AM
 */
@Table(name = "file", catalog = "damate")
@Entity
@PrimaryKeyJoinColumn(name = "parentEntity")
public class File extends ObservedObject {

    private String name;

    @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 255, precision = 0)
    @Basic
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String path;

    @Column(name = "path", nullable = false, insertable = true, updatable = true, length = 65535, precision = 0)
    @Basic
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    private Integer type;

    @Column(name = "type", nullable = false, insertable = true, updatable = true, length = 65535, precision = 0)
    @Basic
    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    private Project project;

    @ManyToOne(fetch = LAZY, cascade = ALL)
    @JoinColumn(name = "project")
    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof File)) return false;
        if (!super.equals(o)) return false;

        File file = (File) o;

        if (!name.equals(file.name)) return false;
        if (!path.equals(file.path)) return false;
        if (!project.equals(file.project)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + path.hashCode();
        result = 31 * result + project.hashCode();
        return result;
    }

    @Transient
    public String getUUID() {
        return generateUUID("" + getObservedEntityId(), getOwner().getUsername(), getProject().getName(), getName(), getDataTimeWithoutNanos(getCreationDate()));
    }
}
